const request = require('../../utils/https.js')
var userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;

    console.log('编辑身份==>', options)

    userId = wx.getStorageSync('userId');
    //oneAdd有值为新增 空值为编辑
    let oneAdd = options.add

    if (oneAdd == 'oneAdd') {
      console.log('新增===>')

    } else {
      console.log('编辑===>')
      let userId = wx.getStorageSync('userId');
      request._get(getApp().Api_url + 'user/hasAuth', {
        userId: userId
      }, function(res) {
        if (res.data.errCode == 20000) {
          console.log('显示信息==>', res.data)
          that.setData({
            username: res.data.data.name,
            identityCode: res.data.data.idCard,
            money: res.data.data.money
          })

        }
      }, function() {})
    }


  },
  getUserName(e) {
    const that = this;
    let userName = e.detail.detail.value;
    that.setData({
      username: userName
    })
  },

  getPhone(e) {
    const that = this;
    let identityCode = e.detail.detail.value;
    that.setData({
      identityCode: identityCode
    })
  },
  // 验证身份证号码 
  isPoneAvailable(str) {
    var myreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (!myreg.test(str)) {
      return false;
    } else {
      return true;
    }
  },
  postSubmit() {
    const that = this;
    var username = that.data.username
    var identityCode = that.data.identityCode


    console.log('username==>', username)
    console.log('identityCode==>', identityCode)

    //判断信息填写完整没有
    if (that.isPoneAvailable(identityCode)) {
      if (username) {
        request._post(getApp().Api_url + 'user/auth', { //认证
          userId: userId,
          name: username,
          idCard: identityCode
        }, function(res) {

          console.log('提交身份证号码===>', res)

          if (res.data.errCode == 200) {
            wx.navigateTo({
              url: '/pages/withdrawal/withdrawal'
            })
          } else {
            wx.showToast({
              title: '提交失败',
              icon: 'none',
              duration: 2000
            })
          }
        }, function() {})
      } else {
        wx.showToast({
          title: '信息不完整！',
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      wx.showToast({
        title: '身份证号码有误，请重新输入！',
        icon: 'none',
        duration: 2000
      })

    }
  },
})